package v1

import (
	"ginblog/api/common"
	"ginblog/model"
	"ginblog/tools/pagination"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// CommentAll /第一级的评论
func CommentAll(c *gin.Context) {
	comment := model.Comment{}
	_ = c.ShouldBindBodyWith(&comment, binding.JSON)

	if comment.ArticleId == 0 {
		common.FailJson(c, errmsg.COMMENT_ARTICLE_ID_IS_NULL)
		return
	}

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	comment.SelectComment(&page)
	if comment.GetCode() != 0 {
		common.FailJson(c, comment.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)
}

// SelectProbeDownComment /查询第一级下探的评论
func SelectProbeDownComment(c *gin.Context) {
	comment := model.Comment{}
	_ = c.ShouldBindBodyWith(&comment, binding.JSON)

	if comment.ArticleId == 0 {
		common.FailJson(c, errmsg.COMMENT_ARTICLE_ID_IS_NULL)
		return
	}
	if comment.ParentId == 0 {
		common.FailJson(c, errmsg.COMMENT_PARENT_ID_IS_NULL)
		return
	}

	page := pagination.Pagination{}

	_ = c.ShouldBindBodyWith(&page, binding.JSON)

	comment.SelectProbeDownComment(&page)
	if comment.GetCode() != 0 {
		common.FailJson(c, comment.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, page)

}

// AddComment /添加评论
func AddComment(c *gin.Context) {
	comment := model.Comment{}
	_ = c.ShouldBindBodyWith(&comment, binding.JSON)
	if comment.ArticleId == 0 {
		common.FailJson(c, errmsg.COMMENT_ARTICLE_ID_IS_NULL)
		return
	}
	data := comment.AddComment()
	if comment.GetCode() != 0 {
		common.FailJson(c, comment.GetCode())
		return
	}
	common.SuccessJson(c, errmsg.SUCCESS, data)
}
